from flask import Flask, jsonify, send_from_directory
from flask_cors import CORS
import random
import time
from datetime import datetime

app = Flask(__name__)
CORS(app)

# 模拟设备数据
devices = {
    'spine-1': {
        'name': 'Spine-1',
        'status': 'normal',
        'ip': '192.168.1.1',
        'model': 'Cisco Nexus 9336C',
        'cpu': 32,
        'memory': 45,
        'temperature': 38,
        'txTraffic': 520,
        'rxTraffic': 480
    },
    'spine-2': {
        'name': 'Spine-2',
        'status': 'normal',
        'ip': '192.168.1.2',
        'model': 'Cisco Nexus 9336C',
        'cpu': 28,
        'memory': 42,
        'temperature': 36,
        'txTraffic': 480,
        'rxTraffic': 510
    },
    'leaf-1': {
        'name': 'Leaf-1',
        'status': 'normal',
        'ip': '192.168.1.11',
        'model': 'Cisco Nexus 93180',
        'cpu': 45,
        'memory': 52,
        'temperature': 42,
        'txTraffic': 320,
        'rxTraffic': 290
    },
    'leaf-2': {
        'name': 'Leaf-2',
        'status': 'normal',
        'ip': '192.168.1.12',
        'model': 'Cisco Nexus 93180',
        'cpu': 38,
        'memory': 48,
        'temperature': 40,
        'txTraffic': 280,
        'rxTraffic': 310
    },
    'leaf-3': {
        'name': 'Leaf-3',
        'status': 'warning',
        'ip': '192.168.1.13',
        'model': 'Cisco Nexus 93180',
        'cpu': 65,
        'memory': 72,
        'temperature': 48,
        'txTraffic': 410,
        'rxTraffic': 390
    },
    'leaf-4': {
        'name': 'Leaf-4',
        'status': 'normal',
        'ip': '192.168.1.14',
        'model': 'Cisco Nexus 93180',
        'cpu': 42,
        'memory': 55,
        'temperature': 41,
        'txTraffic': 290,
        'rxTraffic': 270
    },
    'server-1': {
        'name': 'Server-1',
        'status': 'normal',
        'ip': '192.168.2.101',
        'model': 'Dell PowerEdge R740',
        'cpu': 28,
        'memory': 62,
        'temperature': 35,
        'txTraffic': 120,
        'rxTraffic': 95
    },
    'server-2': {
        'name': 'Server-2',
        'status': 'warning',
        'ip': '192.168.2.102',
        'model': 'Dell PowerEdge R740',
        'cpu': 85,
        'memory': 78,
        'temperature': 42,
        'txTraffic': 210,
        'rxTraffic': 180
    },
    'server-3': {
        'name': 'Server-3',
        'status': 'normal',
        'ip': '192.168.2.103',
        'model': 'Dell PowerEdge R740',
        'cpu': 32,
        'memory': 58,
        'temperature': 36,
        'txTraffic': 95,
        'rxTraffic': 110
    },
    'server-4': {
        'name': 'Server-4',
        'status': 'normal',
        'ip': '192.168.2.104',
        'model': 'Dell PowerEdge R740',
        'cpu': 38,
        'memory': 65,
        'temperature': 38,
        'txTraffic': 150,
        'rxTraffic': 130
    }
}

# 连接关系
connections = [
    {'from': 'spine-1', 'to': 'leaf-1', 'id': 's1-l1'},
    {'from': 'spine-1', 'to': 'leaf-2', 'id': 's1-l2'},
    {'from': 'spine-1', 'to': 'leaf-3', 'id': 's1-l3'},
    {'from': 'spine-1', 'to': 'leaf-4', 'id': 's1-l4'},
    {'from': 'spine-2', 'to': 'leaf-1', 'id': 's2-l1'},
    {'from': 'spine-2', 'to': 'leaf-2', 'id': 's2-l2'},
    {'from': 'spine-2', 'to': 'leaf-3', 'id': 's2-l3'},
    {'from': 'spine-2', 'to': 'leaf-4', 'id': 's2-l4'},
    {'from': 'leaf-1', 'to': 'server-1', 'id': 'l1-sv1'},
    {'from': 'leaf-2', 'to': 'server-2', 'id': 'l2-sv2'},
    {'from': 'leaf-3', 'to': 'server-3', 'id': 'l3-sv3'},
    {'from': 'leaf-4', 'to': 'server-4', 'id': 'l4-sv4'}
]


@app.route('/')
def index():
    return send_from_directory('.', 'index.html')


@app.route('/api/devices')
def get_devices():
    # 模拟数据变化
    for device_id in devices:
        # 随机更新一些指标
        if random.random() > 0.7:
            devices[device_id]['cpu'] = min(100, max(5, devices[device_id]['cpu'] + random.randint(-5, 5)))
            devices[device_id]['memory'] = min(100, max(10, devices[device_id]['memory'] + random.randint(-5, 5)))
            devices[device_id]['temperature'] = max(20, devices[device_id]['temperature'] + random.randint(-2, 2))

    return jsonify(devices)


@app.route('/api/connections')
def get_connections():
    # 为每个连接生成随机流量数据
    connection_data = {}
    for conn in connections:
        traffic = random.randint(50, 500)
        connection_data[conn['id']] = {
            'from': conn['from'],
            'to': conn['to'],
            'traffic': traffic
        }

    return jsonify(connection_data)


@app.route('/api/global-stats')
def get_global_stats():
    total_devices = len(devices)
    online_devices = total_devices  # 简化处理，假设所有设备在线

    total_cpu = sum(devices[device_id]['cpu'] for device_id in devices)
    avg_cpu = round(total_cpu / total_devices, 1)

    total_memory = sum(devices[device_id]['memory'] for device_id in devices)
    avg_memory = round(total_memory / total_devices, 1)

    total_traffic = sum(devices[device_id]['txTraffic'] + devices[device_id]['rxTraffic'] for device_id in devices)
    total_traffic_gbps = round(total_traffic / 1000, 2)

    return jsonify({
        'totalDevices': total_devices,
        'onlineDevices': online_devices,
        'avgCpu': avg_cpu,
        'avgMemory': avg_memory,
        'totalTraffic': total_traffic_gbps,
        'lastUpdate': datetime.now().strftime('%H:%M:%S')
    })


if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5001)